package com.wang.rabbitmq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RabbitMq {

    private static ConnectionFactory connectionFactory(String host, String port, String username, String password) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(host + ":" + port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }

    public static RabbitTemplate buildMessageSender(String host, String port, String username, String password, String exchange, String routingKey) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = connectionFactory(host, port, username, password);
//        Connection connection = connectionFactory.createConnection();

//        if (ExchangeTypes.DIRECT.equals(type)) {
//            buildQueue(exchange, routingKey, queue, connection, "direct");
//        } else if (ExchangeTypes.TOPIC.equals(type) || ExchangeTypes.FANOUT.equals(type)) {
//            buildDeclare(exchange, connection,type);
//        }

        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);

        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setExchange(exchange);
        rabbitTemplate.setRoutingKey(routingKey);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());

        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (!ack) {
                System.out.println("send message failed:1 " + cause + correlationData.toString());
            } else {
                System.out.println("send message 成功: " + cause + correlationData.toString());
            }
        });
        return rabbitTemplate;
    }

    public static void buildQueue(String exchange, String routingKey,
                            String queue, Connection connection, String type) throws IOException, TimeoutException {
        Channel channel = connection.createChannel(false);

        if (type.equals("direct")) {
            channel.exchangeDeclare(exchange, "direct", true, false, null);
        }

        channel.queueDeclare(queue, true, false, false, null);
        channel.queueBind(queue, exchange, routingKey);
        channel.close();
    }

    public static void buildDeclare(String exchange, Connection connection,String exchangeType) throws IOException {
        Channel channel = connection.createChannel(false);
        channel.exchangeDeclare(exchange, exchangeType, true, false, null);
    }
}
